package com.example.qxfw.slhx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.example.qxfw.common.entity.zy.HxyjWarning;
import com.example.qxfw.slhx.mapper.HxyjWarningMapper;
import com.example.qxfw.slhx.service.HxfbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
public class HxfbServiceImpl implements HxfbService {
    @Autowired
    private HxyjWarningMapper hxyjWarningMapper;

    @Override
    public List<HxyjWarning> getAllHxyjWarning() {
        return hxyjWarningMapper.selectList(null);
    }

    @Override
    public List<HxyjWarning> selectStateOfHxyjWarning() {
        return hxyjWarningMapper.selectList(new LambdaQueryWrapper<HxyjWarning>()
        .ne(HxyjWarning::getState, 2));
    }

    @Override
    public List<HxyjWarning> WarnPublishOfHxyjWarning(String warmID, String third) {
        if (warmID == null||warmID.equals("") ){
            throw new IllegalArgumentException("Id不能为空");
        }else {
            //根据点击获取记录
            List<HxyjWarning> list = hxyjWarningMapper.selectList(new LambdaQueryWrapper<HxyjWarning>()
                    .eq(StringUtils.isNotBlank(warmID), HxyjWarning::getWarningId, warmID));
            //修改发布状态和作者，并生成文档路径
            for (HxyjWarning hxyjWarning : list) {
                //修改发布状态
                hxyjWarning.setState(hxyjWarning.getState() != 2 ? 2 : hxyjWarning.getState());
                //修改作者
                if (!third.equals("") || third != null){
                    hxyjWarning.setThird(third);
                }
                //修改文档路径名称（这里不知道文档发布在了哪里可能是数据库）先暂时修改状态
                if (hxyjWarning.getDocpath() == null || hxyjWarning.getDocpath().equals("")) {
                    // 获取当前时间
                    LocalDateTime currentDateTime = LocalDateTime.now();
                    // 定义日期时间格式
                    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年M月d日");
                    // 格式化日期时间并输出
                    String formattedDateTime = currentDateTime.format(formatter);
                    String date = String.valueOf(formattedDateTime);
                    String text = "第" + hxyjWarning.getWarningId() + "期森林火险预报.doc";
                    hxyjWarning.setDocpath(date + text);
                }
                //更新
                hxyjWarningMapper.update(hxyjWarning, new LambdaUpdateWrapper<HxyjWarning>()
                        .eq(StringUtils.isNotBlank(warmID), HxyjWarning::getWarningId, warmID));

            }
            // 返回更新后的预警信息列表
            return list;
        }
    }
}
